package com.allwees.bs.module.oms.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.allwees.bs.core.model.order.entity.OrderItemModel;
import com.allwees.bs.core.model.order.entity.OrderRefundModel;
import com.allwees.bs.module.oms.req.PlatFormCancelShippedReq;
import com.allwees.bs.module.oms.req.RetailerCancelShippedReq;
import com.allwees.core.common.util.MoneyUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.Transient;

@Data
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
@NoArgsConstructor
@TableName("o_order_refund")
public class OrderRefund extends OrderRefundModel {

    @Transient
    @TableField(exist=false)
    private OrderRefundItem item;


    public OrderRefund(PlatFormCancelShippedReq req, Order order, OrderItem orderItem) {
        this.setOrderUuid(order.getUuid());
        this.setOrderNo(order.getNo());
        this.setRemark(req.getRemarks());
        this.setPhotos(null);
        this.setReason(req.getReason());
        this.setType(req.getType());
        this.setUserUuid(order.getUserUuid());
        this.productUuid = orderItem.getProductUuid();
        this.setDefNo(orderItem.getDefNo());
//        this.setOrderState(orderItem.getState());

        orderItem(orderItem);
    }

    public OrderRefund(RetailerCancelShippedReq req, Order order, OrderItem orderItem) {
        this.setOrderUuid(order.getUuid());
        this.setOrderNo(order.getNo());
        this.setRemark(req.getRemarks());
        this.setPhotos(StringUtils.join(req.getPhotos(), ","));
        this.setReason(req.getReason());
        this.setType(req.getType());
        this.setUserUuid(order.getUserUuid());
        this.productUuid = orderItem.getProductUuid();
        this.setDefNo(orderItem.getDefNo());
//        this.setOrderState(orderItem.getState());

        orderItem(orderItem);
    }

    public OrderRefund orderItem(OrderItemModel orderItem) {
        this.setRefundAmt(orderItem.getAmt()); //paypal实付
        this.setOrderItemUuid(orderItem.getUuid());
        this.setRetailerUuid(orderItem.getRetailerUuid());
        this.setSupplierUuid(orderItem.getSupplierUuid());
        this.setStoreUuid(orderItem.getStoreUuid());
        //退款总金额 = paypal或者credit card 支付金额 +  cash支付金额
        this.setOriginalRefundAmt(orderItem.getOriginalAmt());
        //原始商品金额
        this.setOriginalAmtSku(orderItem.getOriginalAmtSku());
        //原始运费金额
        this.setOriginalAmtShipping(orderItem.getOriginalAmtShipping());

        this.setRefundTotalAmt(MoneyUtil.add(orderItem.getAmt(),orderItem.getAmtCashSku()));
        this.setRefundProductAmt(orderItem.getAmtSku());
        this.setRefundShippingAmt(orderItem.getAmtShipping());
        this.setPayType(MoneyUtil.isNullOrZero(orderItem.getAmt())? "coupon":"payby");
        this.setRefundAmtCash(orderItem.getAmtCashSku());
        this.setRefundAmtPaypal(orderItem.getAmt());
        return this;
    }
}
